package com.longxi.tianjintianbao.ui.presenter.goodstraffic;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SelectGoodsTrafficListListListener;
import com.longxi.tianjintianbao.model.GoodsTraffic;
import com.longxi.tianjintianbao.ui.activity.goodstraffic.SignGoodsReleaseActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.GoodsTrafficListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.view.goodstraffic.IGoodsTrafficListFgView;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GoodsTrafficListFgPresenter extends BasePresenter<IGoodsTrafficListFgView> {

    private MultiTypeAdapter adapter;
    private Items items;

    private String mType = "";

    public GoodsTrafficListFgPresenter(BaseActivity context, LazyFragment fragment) {
        super(context, fragment);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(GoodsTraffic.class, new GoodsTrafficListViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getVisitorList(int page, String state){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("state", state);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getGoodsReleaseList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<GoodsTraffic>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                mFragment.showError();
            }

            @Override
            public void onNext(HttpResult<GoodsTraffic> result) {
                items = NListRefresh.getInstance().fjData(result,
                    getView().getRefreshLayout(),
                    items,
                    adapter,
                    mFragment);
            }
        });
    }

    SelectGoodsTrafficListListListener listener = new SelectGoodsTrafficListListListener() {
        @Override
        public void onSuccess(GoodsTraffic goodsTraffic) {
            Intent mainIntent = new Intent(mContext, SignGoodsReleaseActivity.class);
            mainIntent.putExtra(AppConst.BILLID,goodsTraffic.getBillid());
            mainIntent.putExtra(AppConst.STATE,mFragment.getArguments().getString("state"));
            mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_SIGN_VISITOR);
        }
    };


    public void onDestroy() {
        listener = null;
    }
}
